package com.it.controller;

import com.it.timer.TimerClass;
import org.apache.log4j.Logger;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.Date;

@Controller
@RequestMapping("timer.html")
public class TimeTaskController {
    Logger log = Logger.getLogger(TimeTaskController.class);

    @Autowired
    CronTriggerFactoryBean cronTriggerFactoryBean;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping
    @ResponseBody
    public String f1(String exp) throws SchedulerException, ParseException {
        Scheduler scheduler = (Scheduler) applicationContext.getBean("timerStart");
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger("timerTestTrigger", Scheduler.DEFAULT_GROUP);
        trigger.setCronExpression("*/1 * * * * ?");
//        CronTrigger object = cronTriggerFactoryBean.getObject();
        scheduler.rescheduleJob("timerTestTrigger", Scheduler.DEFAULT_GROUP, trigger);

        return "name";

    }

//    public void testTask() {
//        String taskName = "testTask";
//       String cron = "1 * * * * ?";
//        //判断数据库中是否已经保存了定时时间的配置，有就从数据库中取出赋值给cron,没有就将默认cron的值存入数据库
//
//        //开始定时任务
//        dynamicTaskConfig.startCron("timerTestTrigger", new Runnable() {
//            @Override
//            public void run() {
//                //业务逻辑
//                System.out.println("执行业务逻辑");
//            }
//        }, cron);
//    }

//    public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) {
//        try {
//            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName);
//            if (trigger == null) {
//                return;
//            }
//            String oldTime = trigger.getCronExpression();
//            if (!oldTime.equalsIgnoreCase(time)) {
//                CronTrigger ct = (CronTrigger) trigger;
//                // 修改时间
//                ct.setCronExpression(time);
//                // 重启触发器
//                sched.resumeTrigger(triggerName, triggerGroupName);
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
}
